namespace MxWeiXinPF.WeiXinComm
{
    using MxWeiXinPF.DBUtility;
    using MxWeiXinPF.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class wxResponse_dt_user_BaseData
    {
        public static int Add(int wid, string openid, int subscribe, string nickname, int sex, string language, string city, string province, string country, string headimgul, string remark, string unionid)
        {
            try
            {
                MxWeiXinPF.Model.wx_userinfo_BaseData model = new MxWeiXinPF.Model.wx_userinfo_BaseData {
                    wid = wid,
                    openid = openid,
                    subscribe = subscribe,
                    nickname = nickname,
                    sex = new int?(sex),
                    language = language,
                    city = city,
                    province = province,
                    country = country,
                    headimgul = headimgul,
                    remark = remark,
                    unionid = unionid
                };
                if (subscribe == 1)
                {
                    return AddUserInfo(model);
                }
                return UpdateUserInfo(model);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static int AddUserInfo(MxWeiXinPF.Model.wx_userinfo_BaseData model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into dt_users(");
            builder.Append("[wid],[wxOpenId],[subscribe],[nick_name],wxName,[sex],[language],[city],[province],[country],[avatar],[subscribe_time],[unsubscribe_time],[remark],[unionid],[isweixin],[group_id])");
            builder.Append(" values (");
            builder.Append("@wid,@openid,@subscribe,@nickname,@nickname,@sex,@language,@city,@province,@country,@headimgul,getdate(),null,@remark,@unionid,'1','1')");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@wid", SqlDbType.Int, 4), new SqlParameter("@openid", SqlDbType.VarChar, 100), new SqlParameter("@subscribe", SqlDbType.Int, 4), new SqlParameter("@nickname", SqlDbType.VarChar, 50), new SqlParameter("@sex", SqlDbType.Int, 4), new SqlParameter("@language", SqlDbType.VarChar, 50), new SqlParameter("@city", SqlDbType.NVarChar, 20), new SqlParameter("@province", SqlDbType.NVarChar, 20), new SqlParameter("@country", SqlDbType.NVarChar, 50), new SqlParameter("@headimgul", SqlDbType.VarChar, 500), new SqlParameter("@remark", SqlDbType.VarChar, 500), new SqlParameter("@unionid", SqlDbType.VarChar, 100) };
            cmdParms[0].Value = model.wid;
            cmdParms[1].Value = model.openid;
            cmdParms[2].Value = model.subscribe;
            cmdParms[3].Value = model.nickname;
            cmdParms[4].Value = model.sex;
            cmdParms[5].Value = model.language;
            cmdParms[6].Value = model.city;
            cmdParms[7].Value = model.province;
            cmdParms[8].Value = model.country;
            cmdParms[9].Value = model.headimgul;
            cmdParms[10].Value = model.remark;
            cmdParms[11].Value = model.unionid;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public static int UpdateUserInfo(MxWeiXinPF.Model.wx_userinfo_BaseData model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update wx_Userinfo set");
            builder.Append("[subscribe]=@subscribe,[subscribe_time]=null,[unsubscribe_time]=getdate()");
            builder.Append(" where wid =@wid and openid =@openid");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@wid", SqlDbType.Int, 4), new SqlParameter("@openid", SqlDbType.VarChar, 100), new SqlParameter("@subscribe", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.wid;
            cmdParms[1].Value = model.openid;
            cmdParms[2].Value = model.subscribe;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }
    }
}

